package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.User;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.javassist.bytecode.stackmap.BasicBlock;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.jy.wms.service.WmsTurnoverBoxService;
import javax.servlet.http.HttpSession;
import com.github.pagehelper.Page;

import java.util.List;
import com.jy.wms.common.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @ClassName: WmsTurnoverBoxController
 * @Description: 周转箱信息
 * @Author yjs
 * @Date Wed Oct 11 11:02:31 GMT+08:00 2017
 */
@RestController
@RequestMapping("/wmsturnoverbox")
@Api(value = "/wmsturnoverbox", description = "关于周转箱信息操作")
public class  WmsTurnoverBoxController extends BaseController<WmsTurnoverBoxService,WmsTurnoverBox> {
    /**
     *
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value = {"list", ""})
    @ApiOperation(value = "查询周转箱信息",response = JSONObject.class, notes = "查询周转箱信息")
    public @ResponseBody  JSONObject getWmsTurnoverBox(@RequestBody WmsTurnoverBox entity,HttpSession session) {
        Page<WmsTurnoverBox> list= new Page<WmsTurnoverBox>();
        JSONObject json = getJSONObject();
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            entity.setCkid(ckid);
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            if(hz.size()>0){
                entity.setSshzid(hz.get(0).getHzid());
                list= baseService.baseSelectPage(entity);
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 删除周转箱信息
     * @param setModel
     * @return
     */
    @PostMapping(value = "delete")
    @ApiOperation(value = "删除周转箱信息",response = JSONObject.class, notes = "删除周转箱信息")
    public @ResponseBody  JSONObject deleteWmsTurnoverBox(@ApiParam(required = true, name = "setModel", value = "配置参数映射对象") @RequestBody SetAndModifyModel setModel) {
        JSONObject json = getJSONObject();
        try{
            baseService.baseDeleteBathPrimaryKeys(setModel.getIds());
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "删除周转箱信息成功", "", "");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"删除周转箱信息失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"删除周转箱信息失败","删除周转箱信息失败","");
        }
        return json;
    }

    /**
     * 新增周转箱信息
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value = "add")
    @ApiOperation(value = "新增周转箱信息",response = JSONObject.class, notes = "新增周转箱信息")
    public @ResponseBody  JSONObject addWmsTurnoverBox(@RequestBody WmsTurnoverBox entity,HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            entity.setCkid(ckid);
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            if(hz.size()>0){
                entity.setSshzid(hz.get(0).getHzid());
            }
            WmsTurnoverBox entity_s=baseService.addSearch(entity);
            if(entity_s!=null){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.info.toString(),"新增周转箱信息重复","新增周转箱重复","");
            }else {
                entity.setId(UUIDUtils.uuid());
                //最大体积计算(长*宽*高)
                String zdtj = String.valueOf(Double.parseDouble(entity.getC()) * Double.parseDouble(entity.getK()) * Double.parseDouble(entity.getG()));
                entity.setZdtj(zdtj);
                //可用体积计算(最大体积*装载率)
                String kytj = String.valueOf(Double.parseDouble(zdtj) * Double.parseDouble(entity.getZzl()));
                entity.setKytj(kytj);
                entity.setXs("0");
                String sfjh=entity.getSfjh();
                if("1".equals(sfjh)){
                    entity.setShow_sfjh("已激活");
                }else if("0".equals(sfjh)){
                    entity.setShow_sfjh("未激活");
                }
                if (!"".equals(entity.getFdrj()) && entity.getFdrj() != null) {
                } else {
                    entity.setFdrj("0");
                }
                baseService.baseInsert(entity);
                json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "新增周转箱信息成功", "", JSONObject.fromObject(entity).toString());
            }

        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"新增周转箱信息失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增周转箱信息失败","新增周转箱信息失败","");
        }
        return json;
    }

    /**
     * 查看周转箱信息
     * @param id
     * @return
     */
    @PostMapping(value = "getWmsTurnoverBox/{id}")
    @ApiOperation(value = "查看周转箱信息",response = JSONObject.class, notes = "POST查看周转箱信息")
    public @ResponseBody  JSONObject getWmsTurnoverBoxById(@PathVariable("id") String id) {
        JSONObject json = getJSONObject();
        try {
            WmsTurnoverBox entity=baseService.baseSelectByPrimaryKey(id);
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "查看周转箱信息成功", "", JSONObject.fromObject(entity).toString());
        } catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看周转箱信息失败","查看周转箱信息失败","");
        }
        return json;
    }

    /**
     * 编辑周转箱信息
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value = "edit")
    @ApiOperation(value = "编辑周转箱信息",response = JSONObject.class, notes = "编辑周转箱信息")
    public @ResponseBody  JSONObject editWmsTurnoverBox(@RequestBody WmsTurnoverBox entity,HttpSession session) {
        JSONObject json = getJSONObject();
        try{
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            entity.setCkid(ckid);
            if(hz.size()>0){
                entity.setSshzid(hz.get(0).getHzid());
            }
            WmsTurnoverBox entity_s=baseService.addSearch_s(entity);
            if(entity_s!=null){
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.info.toString(),"编辑周转箱信息重复","修改周转箱重复","");
            }else {
                //可用体积计算(最大体积*装载率)
                String kytj = String.valueOf(Double.parseDouble(entity.getZdtj()) * Double.parseDouble(entity.getZzl()));
                entity.setKytj(kytj);
                String sfjh=entity.getSfjh();
                if("1".equals(sfjh)){
                    entity.setShow_sfjh("已激活");
                }else if("0".equals(sfjh)){
                    entity.setShow_sfjh("未激活");
                }
                baseService.baseUpdateByPrimaryKeySelective(entity);
            }
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "编辑周转箱信息成功", "", JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"编辑周转箱信息失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"编辑周转箱信息失败","编辑周转箱信息失败","");
        }
        return json;
    }

    /**
     * 模糊查询货主信息
     * @param session
     * @return
     */
    @PostMapping(value = "searchHz")
    @ApiOperation(value = "模糊查询货主信息",response = JSONObject.class, notes = "POST模糊查询货主信息")
    public @ResponseBody JSONObject searchHz(HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);//货主信息
            if(hz.size()>0){
                json.put("show_sshzid",hz.get(0).getShow_hzid());//货主名称
                json.put("sshzid",hz.get(0).getHzid());//货主编号
            }else {
                json.put("show_sshzid","");//货主名称
                json.put("sshzid","");//货主编号
            }
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            return json;
        }
    }

    /**
     * 新增箱数信息
     * @param entitys
     * @param session
     * {"xgyy":"","id":"","sl":""}
     * @return
     */
    @PostMapping(value = "addSl")
    @ApiOperation(value = "新增箱数信息",response = JSONObject.class, notes = "新增箱数信息")
    public @ResponseBody  JSONObject addSl(@RequestBody WmsTurnoverBox entitys, HttpSession session) {
        JSONObject json = getJSONObject();
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
        User user=userinfo.getUser();
        WmsTurnoverBox entity=baseService.baseSelectByPrimaryKey(entitys.getId());
        try{
            entitys.setZzxbd_id(UUIDUtils.uuid());//周转箱变动表编号
            entitys.setYsl(entity.getXs());//修改前数量
            String xs=String.valueOf(Integer.parseInt(entity.getXs())+Integer.parseInt(entitys.getSl()));//修改后数量
            Integer zzxs=Integer.parseInt(xs);
            Integer zzxss=0;
            if(zzxs>=zzxss){
                entitys.setXgsl(xs);
                entitys.setXs(xs);
                entitys.setCzr(String.valueOf(user.getId()));//操作人
                entitys.setCkid(ckid);
                baseService.addSl(entitys);
            }else{
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "新增周转箱信息失败", "箱数不能小于零", "");
            }
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "新增周转箱信息成功", "", JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"新增周转箱信息失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增周转箱信息失败","新增箱数失败","");
        }
        return json;
    }

    /**
     * 扣减箱数信息
     * @param entitys
     * @param session
     * {"xgyy":"","xs":"","id":"","sl":""}
     * @return
     */
    @PostMapping(value = "delSl")
    @ApiOperation(value = "扣减箱数信息",response = JSONObject.class, notes = "扣减箱数信息")
    public @ResponseBody  JSONObject delSl(@RequestBody WmsTurnoverBox entitys, HttpSession session) {
        JSONObject json = getJSONObject();
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
        User user=userinfo.getUser();
        WmsTurnoverBox entity=baseService.baseSelectByPrimaryKey(entitys.getId());
        try{
            entitys.setZzxbd_id(UUIDUtils.uuid());//周转箱变动表编号
            entitys.setYsl(entity.getXs());//修改前数量
            String xs=String.valueOf(Integer.parseInt(entity.getXs())-Integer.parseInt(entitys.getSl()));//修改后数量
            Integer zzxs=Integer.parseInt(xs);
            Integer zzxss=0;
            if(zzxs<zzxss) {
                json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "扣减周转箱箱数失败", "扣减箱数不能大于原箱数", "");
            }else{
                entitys.setXgsl(xs);
                entitys.setXs(xs);
                entitys.setCkid(ckid);
                entitys.setCzr(String.valueOf(user.getId()));//操作人
                baseService.addSl(entitys);
            }
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "扣减周转箱箱数成功", "", JSONObject.fromObject(entity).toString());
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"扣减周转箱箱数失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"扣减周转箱箱数失败","扣减箱数失败","");
        }
        return json;
    }
    /**
     * 已激活接口
     * @param setModel
     * @return
     */
    @PostMapping(value = "yjh")
    @ApiOperation(value = "激活操作",response = JSONObject.class, notes = "数据激活操作")
    public @ResponseBody  JSONObject yjh(@RequestBody SetAndModifyModel setModel) {
        JSONObject json = getJSONObject();
        try{
            //1:激活
            baseService.sfjh(setModel.getIds(),"1");
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "已激活操作操作成功", "", "");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"已激活操作操作失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"已激活操作操作失败","操作失败","");
        }
        return json;
    }
    /**
     * 未激活接口
     * @param setModel
     * @return
     */
    @PostMapping(value = "bjh")
    @ApiOperation(value = "取消激活操作",response = JSONObject.class, notes = "数据取消激活操作")
    public @ResponseBody  JSONObject bjh(@RequestBody SetAndModifyModel setModel) {
        JSONObject json = getJSONObject();
        try{
            //0:未激活状态
            baseService.sfjh(setModel.getIds(),"0");
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "取消激活操作成功", "", "");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"取消激活操作失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"取消激活操作失败","操作失败","");
        }
        return json;
    }
}
